package pCloud;

import pCloud.server.DataStreamHandle;
import pCloud.server.DatabaseHandle;
import pCloud.service.AccessControlHandle;
import pCloud.service.AuroraDataStreamHandle;
import pCloud.service.ProtocolHandle;
import pCloud.transport.EndPointTransportHandle;
import pCloud.transport.TransportHandle;

/**
 * @author anhdinh
 * 
 *         Factory to initiate classes specified in the Configuration. The
 *         common configurable classes are: 1. PEP and PDP 2. DatabaseConnector
 *         3. ProtocolHandle and TransportHandle
 */
public class PCloudFactory {

	public static TransportHandle getNewTransportHandleInstance(String className)
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		Object o = getObjectFromClass(className);
		if (o instanceof TransportHandle)
			return (TransportHandle) o;
		else
			return null;
	}

	public static EndPointTransportHandle getEndPointTransportHandle(
			String className) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException {
		Object o = getObjectFromClass(className);
		if (o instanceof EndPointTransportHandle)
			return (EndPointTransportHandle) o;
		else
			return null;
	}

	public static ProtocolHandle getNewProtocolHandleInstance(String className)
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		Object o = getObjectFromClass(className);
		if (o instanceof ProtocolHandle)
			return (ProtocolHandle) o;
		else
			return null;
	}

	public static DatabaseHandle getNewDatabaseHandleInstance(String className)
			throws Exception {
		Object o = getObjectFromClass(className);
		if (o instanceof DatabaseHandle)
			return (DatabaseHandle) o;
		else
			return null;
	}

	public static AccessControlHandle getNewAccessControlHandleInstance(
			String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		Object o = getObjectFromClass(className);
		if (o instanceof AccessControlHandle)
			return (AccessControlHandle) o;
		else
			return null;
	}
	
	// by wwq : Stream Data Handle
	
	public static DataStreamHandle getNewDataStreamHandle(String className) throws Exception {
		Object o = getObjectFromClass(className);
		if (o instanceof DataStreamHandle)
			return (DataStreamHandle) o;
		else 
			return null;
	}
	
	private static Object getObjectFromClass(String className)
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		Class cl = Class.forName(className);
		return cl.newInstance();
	}
}
